package 代码随想录_补充题目.哈希双映射;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zx
 * @create 2023-02-10 23:29
 */
public class 单词规律_290 {
    public boolean wordPattern(String pattern, String s) {
        String[] arr = s.split(" ");
        if(arr.length != pattern.length()){
            return false;
        }
        Map<Character,String> map1 = new HashMap<>();
        Map<String,Character> map2 = new HashMap<>();
        for(int i = 0;i < pattern.length();i++){
            if(!map1.containsKey(pattern.charAt(i))){
                map1.put(pattern.charAt(i),arr[i]);
            }
            if(!map2.containsKey(arr[i])){
                map2.put(arr[i],pattern.charAt(i));
            }
            //注意这里必须使用equals(),不能像205那样
            if(!map1.get(pattern.charAt(i)).equals(arr[i]) ||
                    map2.get(arr[i]) != pattern.charAt(i)){
                return false;
            }
        }
        return true;
    }
}
